package de.hwrBerlin.dataMining.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabItem;

import de.hwrBerlin.dataMining.constants.Images;
import de.hwrBerlin.dataMining.constants.UIConstants;
import de.hwrBerlin.dataMining.controller.Controller;
import de.hwrBerlin.dataMining.controller.observer.Observer;
import de.hwrBerlin.dataMining.dataStructure.DecisionTreeNode;
import de.hwrBerlin.dataMining.dataStructure.TableData;
import de.hwrBerlin.dataMining.dataStructure.TreeNode;

public class TreeView extends Composite implements Observer {

	private TreeZoomView _treeZoomView;
	private boolean _editable;
	private TreeDetails _treeDetails;
	private Button _setNewRoot;

	public TreeView(Composite pParent, int pStyle, TabItem pTabItem, boolean pEditable, final Controller pController) {
		super(pParent, pStyle);

		_editable = pEditable;

		pController.addObserverToModel(this);
		final TreeView tmpThis = this;
		addDisposeListener(new DisposeListener() {
			@Override
			public void widgetDisposed(DisposeEvent pArg0) {
				pController.removeObserverToModel(tmpThis);
			}
		});

		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 5;
		layout.marginHeight = 5;
		setLayout(layout);

		Composite optionComposite = new Composite(this, SWT.BORDER);
		optionComposite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
		fillLayout.spacing = 2;
		fillLayout.marginWidth = 1;
		fillLayout.marginHeight = 1;
		optionComposite.setLayout(fillLayout);

		Composite buttonComposite = new Composite(optionComposite, SWT.NONE);
		buttonComposite.setLayout(new GridLayout(7, false));

		if (pEditable) {
			_setNewRoot = new Button(buttonComposite, SWT.PUSH);
			_setNewRoot.setImage(new Image(getDisplay(), Images.SET_NEW_TREE_ROOT));
			_setNewRoot.setToolTipText(UIConstants.SET_NEW_TREE_ROOT_TOOL_TIP);
			_setNewRoot.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent pE) {
				    _treeZoomView.getTreePainter().setSelection(null);
					_treeDetails.setInput(null);
					String targetAttribute = ((DecisionTreeNode)_treeZoomView.getTreePainter().getTree().getRoot()).getTargetAttribute();
					((DecisionTreeNode)_treeZoomView.getTreePainter().getTree()).setOwnAttribute(targetAttribute);
				}
			});
			_setNewRoot.setEnabled(false);
		}

		Button setFullScreen = new Button(buttonComposite, SWT.PUSH);
		setFullScreen.setImage(new Image(getDisplay(), Images.FULL_SCREEN_MODE));
		setFullScreen.setToolTipText(UIConstants.FULL_SCREEN_TOOL_TIP);
		setFullScreen.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent pE) {
				new FullScreenTreeView(getDisplay(), SWT.NONE, _treeZoomView.getTreePainter().getTree());
			}
		});
		
		SashForm sashForm = new SashForm(this, SWT.NONE);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		
		// TreePainter und Zoom
        _treeZoomView = new TreeZoomView(sashForm, SWT.BORDER, this);
        _treeZoomView.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        
        

		_treeDetails = new TreeDetails(sashForm, SWT.BORDER, pEditable);

		pTabItem.setControl(this);
	}

	public boolean isEditable() {
		return _editable;
	}

	public void selectNode(DecisionTreeNode pFindNodeAtPosition) {
		_treeDetails.setInput(pFindNodeAtPosition);
	}

	@Override
	public void updateAllTable(TableData pTableData) {
	}

	@Override
	public void updateTree(TreeNode pTree) {
	}

	@Override
	public void updateAllInteractiveTree(TreeNode pTree) {
		if (_editable) {
			if (pTree != null){
			    if(!pTree.getNodeList().contains(_treeZoomView.getTreePainter().getSelection())){
			        _treeZoomView.getTreePainter().setSelection(null);
			    }
				_setNewRoot.setEnabled(true);
			}
			_treeDetails.setInput((DecisionTreeNode) _treeZoomView.getTreePainter().getSelection());
			_treeZoomView.getTreePainter().repaint(pTree);
			_treeZoomView.getTreePainter().redraw();
		}		
	}
	@Override
	public void updateAllAutomaticTree(TreeNode pTree) {
		if (!_editable) {
		    if(pTree != null){
	            if(!pTree.getNodeList().contains(_treeZoomView.getTreePainter().getSelection())){
	                _treeZoomView.getTreePainter().setSelection(null);
	            }
		    }
			_treeDetails.setInput((DecisionTreeNode) _treeZoomView.getTreePainter().getSelection());
			_treeZoomView.getTreePainter().repaint(pTree);
			_treeZoomView.getTreePainter().redraw();
		}
	}
}
